package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Autowired(required = false)
     private CategoryMapper categoryMapper;

    //开启事务
    @Transactional
    @Override
    public boolean save(SetmealDto setmealDto) {
        //完善套餐数据(状态,创建时间,跟新时间)
        setmealDto.setStatus(1);
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());

        setmealMapper.save(setmealDto);

        //遍历套餐菜品列表完善数据
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes().stream().map((setmealDish -> {
            //(设置套餐主题,创建人 创建时间 跟新人 跟新时间 排序)
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            return setmealDish;

        })).collect(Collectors.toList());
        setmealDishMapper.save(setmealDishList);



        return true;
    }
//套餐的分页查询数据
    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        //2执行查询所有的套餐数据
        //经过PageHelper处理以后返回当前页面数据列表
        List<Setmeal> setmealList = setmealMapper.findAll(name);
        //3封装层[PageInfo对象
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);
        //4处理当前页数数据列表中每个套餐setmeal对象
        //根据里面的类别id查询名称,最后将所有的封装给SetmealDto

        //将List<Setmeal>当前的数据列表 == list<SetmealDto>
        List<SetmealDto> setmealDtoList= setmealList.stream().map((setmeal -> {

            SetmealDto setmealDto = new SetmealDto();

            //将List<Setmeal>当前的数据列表 == list<SetmealDto>
            BeanUtils.copyProperties(setmeal,setmealDto);
            //获取类别名
            Category category = categoryMapper.findById(setmealDto.getCategoryId());
            //将类别名称赋值给setmealDto
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        })).collect(Collectors.toList());


        //封装成前端需要的Page<SetmealDto>对象
        Page<SetmealDto> pageObj = new Page<>();
        pageObj.setRecords(setmealDtoList);
        pageObj.setTotal(pageInfo.getTotal());


        return pageObj;
    }
    @Transactional//开启事务

    @Override
    public boolean deleteByid(List<Long> ids, Long updateUser) {
        //判断传递过来的套餐id列表列表是否存在启用的套餐
        //查询启动的个数,个数大于0代表存在,否则不存在
        Integer count = setmealMapper.countByIds(ids);
        //如果存在就抛出无法删除的异常
        if(count>0){
            throw new BusinessException("无法删除启用的套餐");
        }
        //3.调用访问层进行批量删除,传递修稿人,最新的修改时间
        //批量删除套餐菜品关心数据
        //否则走下面的步骤
        setmealDishMapper.deleteBySetmealIds(ids,updateUser,LocalDateTime.now());
        //批量删除套餐
        setmealMapper.deleteByIds(ids,updateUser,LocalDateTime.now());

        //返回数据

        return true;
    }
}
